const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function strDateTime(timestamp){
  var strDateTime = formatTimeStamp(timestamp, 'Y-M-D') + ' 00:00';
  return strDateTime;
}

function endDateTime(timestamp) {
  var endDateTime = formatTimeStamp(timestamp, 'Y-M-D') + ' 23:59';
  return endDateTime;
} 

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/** 
 * 时间戳转化为年 月 日 时 分 秒 
 * number: 传入时间戳 
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致 
*/
function formatTimeStamp(number, format) {
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  if (number == undefined || number == null || number == ''){
    return;
  }
  var returnArr = [];
  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));
  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));
  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

//图片点击事件
const imgYu = event => {
  var src = event.currentTarget.dataset.src;//获取data-src
  var imgList = event.currentTarget.dataset.list;//获取data-list
  //图片预览
  wx.previewImage({
    current: src, // 当前显示图片的http链接
    urls: imgList, // 需要预览的图片http链接列表
    success: function (res) { console.log(res); console.log(111) },
    fail: function (res) { console.log(res); console.log(222) },
    complete: function (res) { console.log(res); console.log(333) }
  })
}

//图片点击事件
const imgYu2 = event => {
  var src = event.currentTarget.dataset.src.realPath;//获取data-src
  var imgList = event.currentTarget.dataset.list;//获取data-list
  var imgLists = [];
  for (var i = 0; i < imgList.length; i++) {
    imgLists.push(imgList[i].realPath);
  }
  console.log(src);
  console.log(imgLists);
  //图片预览
  wx.previewImage({
    current: src, // 当前显示图片的http链接
    urls: imgLists, // 需要预览的图片http链接列表
    success: function (res) { console.log(res); console.log(111) },
    fail: function (res) { console.log(res); console.log(222) },
    complete: function (res) { console.log(res); console.log(333) }
  })
}

//生成uuid
function uuid() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "";

  var uuid = s.join("");
  return uuid;
}

/*const bindDownLoad = function (pageTotal,curPage,_this) {
  //该方法绑定了页面滑动到底部的事件
  var _this = this;
  //判断是否是最后一页，最后一页不加载数据了
  if (pageTotal == curPage) {
    console.log('已经到底部了');
    _this.setData({
      loadMoreText: '已经到底部了',
      loadMoreHidden: true
    })
    return;
  }
 // _this.doPostData(_this, _this.data.params);
}
const scroll = function (event) {
  //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
  this.setData({
    scrollTop: event.detail.scrollTop
  });
}
const refresh = function (event, params,_this) {
  //该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
  params.page = 1;
  _this.setData({
    list: [],
    scrollTop: 0,
    params: params
  });
 // _this.doPostData(_this, _this.data.params);
}
*/

module.exports = {
  formatTime: formatTime,
  imgYu: imgYu,
  formatTimeStamp: formatTimeStamp,
  uuid: uuid,
  strDateTime: strDateTime,
  endDateTime: endDateTime,
  imgYu2: imgYu2
}


